/*
 * Copyright (c) 2010 Sun Microsystems, Inc.
 * Copyright (c) 2011 Oracle Corporation.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 **/

package org.sunspotworld.demo;


import com.sun.spot.core.resources.Resources;
import com.sun.spot.core.resources.transducers.ITriColorLEDArray;
import com.sun.spot.core.util.Utils;
import java.io.IOException;
import com.sun.spot.wot.WebApplication;
import com.sun.spot.wot.http.HttpResponse;
import com.sun.spot.wot.http.HttpRequest;

/**
 *
 * @author vgupta
 */
public class Blinker extends WebApplication {
    private ITriColorLEDArray myLEDs;
    private String rgbStr;

    public Blinker(String str) {
        super(str);
    }
    
    public void init() {
        this.myLEDs = (ITriColorLEDArray)
            Resources.lookup(ITriColorLEDArray.class);
        for (int i = 0; i < myLEDs.size(); i++) {
            myLEDs.getLED(i).setRGB(0,0,0);
            myLEDs.getLED(i).setOff();                // turn off all LEDs
        }
    }

    public synchronized HttpResponse processRequest(HttpRequest request) throws IOException {
        HttpResponse response = new HttpResponse();

        if (request.getMethod().equalsIgnoreCase("POST")) {
            if (request.getBody() == null) {
                rgbStr = "255,0,0";
            } else {
                rgbStr = new String(request.getBody());
            }
            
            new Thread(new Runnable() {
                public void run() {
                    int[] savedRed = new int[myLEDs.size()];
                    int[] savedGreen = new int[myLEDs.size()];
                    int[] savedBlue = new int[myLEDs.size()];
                    boolean[] isOn = new boolean[myLEDs.size()];

                    // Save LED state
                    for (int i = 0; i < myLEDs.size(); i++) {
                        savedRed[i] = myLEDs.getLED(i).getRed();
                        savedGreen[i] = myLEDs.getLED(i).getGreen();
                        savedBlue[i] = myLEDs.getLED(i).getBlue();
                        isOn[i] = myLEDs.getLED(i).isOn();
                    }
                    for (int j = 0; j < 3; j++) {
                        for (int i = 0; i < myLEDs.size(); i++) {
                            try {
                                MyUtils.setColor(myLEDs.getLED(i), rgbStr);
                            } catch (IOException ex) {
                                ex.printStackTrace();
                            }
                        }
                        Utils.sleep(500);
                        for (int i = 0; i < myLEDs.size(); i++) {
                            myLEDs.getLED(i).setOff();
                        }
                        Utils.sleep(500);
                    }
                    
                    // Restore LED state
                    for (int i = 0; i < myLEDs.size(); i++) {
                        myLEDs.getLED(i).setRGB(savedRed[i], savedGreen[i], savedBlue[i]);
                        savedBlue[i] = myLEDs.getLED(i).getBlue();
                        if (isOn[i])
                            myLEDs.getLED(i).setOn();
                        else
                            myLEDs.getLED(i).setOff();
                    }
                }
            }).start();

            response.setStatus(HttpResponse.SC_OK);
            return response;
        } else {
            response.setStatus(HttpResponse.SC_METHOD_NOT_ALLOWED);
            response.setHeader("Allow", "POST");
            response.setHeader("Cache-Control", "max-age=300"); // this will never change

            return response;
        }
    }
}
